package com.mission.shaco.warehouse

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.androidquery.AQuery
import com.androidquery.callback.AjaxCallback
import com.androidquery.callback.AjaxStatus
import com.mission.shaco.warehouse.models.ClientConfig
import com.mission.shaco.warehouse.models.Roles
import com.mission.shaco.warehouse.models.Tool
import kotlinx.android.synthetic.main.activity_login.*
import org.json.JSONObject

class LoginActivity : BasicActivity(), View.OnClickListener {

    var loginThread: Runnable = Runnable {
        try {
            val aq: AQuery = AQuery(this)
            val params = HashMap<String, Any>()
            val expire = (1).toLong()
            var map = JSONObject()

            map.put("userName", txtUsername.text)
            map.put("password", txtPassword.text)

            params.put("datas",map)

            aq.ajax(ClientConfig.getHostUrl(ClientConfig.JOSN_LOGIN_PATH), params,String::class.java, object : AjaxCallback<String>() {

                override fun callback(url: String, result: String, status: AjaxStatus) {
                    if(status.code == 200){
                        var response: JSONObject = JSONObject(result)
                        if(response.getInt("status") == ClientConfig.RESPONSE_STATUS_SUCCESS) {
                            var info = response.getJSONObject("info")
                            var realName = info.getString("realName")
                            var roles = info.getJSONArray("roles")
                            var role = Roles(roles,realName)
                            Tool().serializableObject("role",role)
                            toMain()
                        }else {
                            noticeMessage(response.getString("msg"))
                        }
                    }else{
                        noticeMessage("网络异常")
                    }
                }
            })

        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }

    override fun onClick(v: View?) {

        when(v?.id ){
            btnLogin.id -> {
                if(check()) {
                    var t = Thread(loginThread)
                    t.start()
                }
            }
        }

    }

    fun toMain(){
        var intent: Intent = Intent(this, MainActivity::class.java)
        this.startActivity(intent)
        finish()
    }


    fun check():Boolean{
        if(TextUtils.isEmpty(txtUsername.text)){
            noticeMessage("账号不能为空!")
            return false
        }
        return true
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        init()
    }

    fun init(){
        btnLogin.setOnClickListener(this)
        if(!TextUtils.isEmpty(opreateName)){
            toMain()
        }
    }


}
